//
//  SettingsTVC.m
//  POS
//
//  Created by sigma xu on 5/5/13.
//  Copyright (c) 2013 sigma xu. All rights reserved.
//

#import "SettingsTVC.h"

@interface SettingsTVC ()

@end

@implementation SettingsTVC
@synthesize sales_man,server_url;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad
{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    sales_man = [prefs stringForKey:@"sales_man"];
    server_url = [prefs stringForKey:@"server_url"];
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section ==0) {
        return @"App Information";
    }else if (section ==1) {
        return @"Functions";
    }else{
        return @"";
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section ==0) {
        return 4;
    }else{
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SettingsCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil){
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleValue1
                reuseIdentifier:CellIdentifier];
    }
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn1 setTitle:@"Update Server URL" forState:UIControlStateNormal];
    btn1.frame = CGRectMake(0, 0,145,40);
    [btn1 addTarget:self action:@selector(update_server_url:) forControlEvents:UIControlEventTouchUpInside];
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn2 setTitle:@"Update Sales Man" forState:UIControlStateNormal];
    btn2.frame = CGRectMake(155, 0,145, 40);
    [btn2 addTarget:self action:@selector(update_sales_man:) forControlEvents:UIControlEventTouchUpInside];
    UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn3 setTitle:@"Test Server" forState:UIControlStateNormal];
    btn3.frame = CGRectMake(0, 0,145,40);
    [btn3 addTarget:self action:@selector(test_server:) forControlEvents:UIControlEventTouchUpInside];
    UIButton *btn4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn4 setTitle:@"Sync Server" forState:UIControlStateNormal];
    btn4.frame = CGRectMake(155, 0,145, 40);
    [btn4 addTarget:self action:@selector(sync_data:) forControlEvents:UIControlEventTouchUpInside];
    switch (indexPath.section) {
        case 0:
            if(indexPath.row == 0){
                cell.textLabel.text = @"Server URL";
                cell.detailTextLabel.text = server_url;
            }else if(indexPath.row == 1){
                cell.textLabel.text = @"Sales Man";
                cell.detailTextLabel.text = sales_man;
            }else if(indexPath.row == 2){
                cell.textLabel.text = @"App Version";
                cell.detailTextLabel.text =@"1.0";
            }else if(indexPath.row == 3){
                cell.textLabel.text = @"Developper";
                cell.detailTextLabel.text = @"Sigma";
            }
            break;
        case 1:
            //            cell.textLabel.text = @"View Detail of Each Item";
            [cell.contentView addSubview:btn1];
            [cell.contentView addSubview:btn2];
            break;
        case 2:
            //cell.textLabel.text = @"Post To Server";
            [cell.contentView addSubview:btn3];
            [cell.contentView addSubview:btn4];
            break;
    }
    if (indexPath.section != 0) {
        //        cell.textLabel.textAlignment = UITextAlignmentCenter;
        cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
        [cell setBackgroundColor:[UIColor clearColor]];
        
    }
    return cell;
}
- (void)update_server_url:(id)sender{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @""
                                                   message: @"Please Enter New Server"
                                                  delegate: self
                                         cancelButtonTitle:@"Cancel"
                                         otherButtonTitles:@"Confirm",nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    alert.tag = 1;
    [alert textFieldAtIndex:0].text=@"http://";
    [alert show];
}
- (void)update_sales_man:(id)sender{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @""
                                                   message: @"Please Enter Your Name"
                                                  delegate: self
                                         cancelButtonTitle:@"Cancel"
                                         otherButtonTitles:@"Confirm",nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    alert.tag = 2;
    [alert show];
}
- (void)test_server:(id)sender{
    ServiceConnector *sc = [[ServiceConnector alloc] init];
    [sc test_connection];
}
- (void)sync_data:(id)sender{
    ServiceConnector *sc = [[ServiceConnector alloc] init];
    [sc syncDB];
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    //Update Server URL
    if (alertView.tag == 1) {
        if (buttonIndex == 0) {
            //cancell
        }else if(buttonIndex == 1){
            //ok
            NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
            [prefs setObject:[alertView textFieldAtIndex:0].text forKey:@"server_url"];
            server_url = [alertView textFieldAtIndex:0].text;
        }
    }else if (alertView.tag == 2) {
        if (buttonIndex == 0) {
            //cancell
        }else if(buttonIndex == 1){
            //ok
            NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
            [prefs setObject:[alertView textFieldAtIndex:0].text forKey:@"sales_man"];
            sales_man = [alertView textFieldAtIndex:0].text;
        }
    }
    [self.tableView reloadData];
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

@end
